package com.gitee.dbswitch.admin.controller.privateapi;

import com.gitee.dbswitch.admin.common.annotation.LogOperate;
import com.gitee.dbswitch.admin.common.annotation.TokenCheck;
import com.gitee.dbswitch.admin.common.response.PageResult;
import com.gitee.dbswitch.admin.common.response.Result;
import com.gitee.dbswitch.admin.config.SwaggerConfig;
import com.gitee.dbswitch.admin.model.request.*;
import com.gitee.dbswitch.admin.model.response.APIAssignmentDetailResponse;
import com.gitee.dbswitch.admin.model.response.APIAssignmentInfoResponse;
import com.gitee.dbswitch.admin.model.response.AssignmentDetailResponse;
import com.gitee.dbswitch.admin.model.response.AssignmentInfoResponse;
import com.gitee.dbswitch.admin.service.APITaskService;
import com.gitee.dbswitch.data.util.OKHttpUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * api task 管理
 *
 * @author natural
 */
@Api(tags ={"API任务管理接口"})
@RestController
@RequestMapping(value = SwaggerConfig.API_V1 + "/api-task")
public class APITaskController {

    @Resource
    private APITaskService apiTaskService;

    @TokenCheck
    @LogOperate(name = "创建api任务", description = "'创建api任务的名称为：'+#request.name")
    @ApiOperation(value = "创建")
    @PostMapping(value = "/create", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<APIAssignmentInfoResponse> createAPIAssignment(
            @RequestBody APITaskCreateRequest request) {
        return Result.success(apiTaskService.createAPI(request));
    }

    @TokenCheck
    @ApiOperation(value = "API任务列表")
    @PostMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public PageResult<APIAssignmentInfoResponse> listAPIAssignment(@RequestBody AssignmentSearchRequest request) {
        return apiTaskService.listAll(request);
    }

    @TokenCheck
    @LogOperate(name = "发布任务", description = "'发布任务的ID为：'+#ids")
    @ApiOperation(value = "发布")
    @PostMapping(value = "/deploy", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result deployAPIAssignments(
            @RequestParam(value = "ids") List<Long> ids) {
        apiTaskService.deployAssignments(ids);
        return Result.success();
    }

    @TokenCheck
    @LogOperate(name = "手动执行任务", description = "'手动执行任务的ID为：'+#ids")
    @ApiOperation(value = "手动执行")
    @PostMapping(value = "/run", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result runAPIAssignments(@RequestBody List<Long> ids) {
        apiTaskService.runAPI(ids);
        return Result.success();
    }
    @TokenCheck
    @ApiOperation(value = "任务详情")
    @GetMapping(value = "/detail/id/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<APIAssignmentDetailResponse> detailAPIAssignment(
            @PathVariable("id") Long id) {
        return apiTaskService.detailAssignment(id);
    }

    @TokenCheck
    @ApiOperation(value = "任务信息")
    @GetMapping(value = "/info/id/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<APIAssignmentInfoResponse> infoAPIAssignment(
            @PathVariable("id") Long id) {
        return apiTaskService.infoAssignment(id);
    }

    @TokenCheck
    @LogOperate(name = "下线任务", description = "'下线任务的ID为：'+#ids")
    @ApiOperation(value = "下线")
    @PostMapping(value = "/retire", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result retireAPIAssignments(@RequestParam(value = "ids") List<Long> ids) {
        apiTaskService.retireAssignments(ids);
        return Result.success();
    }

    @TokenCheck
    @LogOperate(name = "修改任务", description = "'修改任务的名称为：'+#request.name")
    @ApiOperation(value = "修改")
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result updateAPIAssignment(@RequestBody APIAssignmentUpdateRequest request) {
        apiTaskService.updateAssignment(request);
        return Result.success();
    }

    @TokenCheck
    @LogOperate(name = "测试http请求", description = "解决vue页面请求第三方接口的跨域问题")
    @ApiOperation(value = "查询")
    @PostMapping(value = "/testHttpRequest", produces = MediaType.APPLICATION_JSON_VALUE)
    public String testHttpRequest(@RequestBody TestHttpRequest request) {
       return apiTaskService.testHttpRequest(request);
    }
}
